<?php 
namespace Home\Controller;
use \Think\Controller;

class QuestionController extends HomeController
{
    public function index(){
        //查询热门问题
        $question = D('user_topic')->where(array('user_id'=>$_SESSION['home']['id']))->select();
        $data =[];
        foreach ($question as $value) {
            $data[] = $value['topic_id'];
        }
        $list = implode(',',$data);

        if ($list == null) {
            $list = '1,2,3,4,5';
        }

        $map['topic_id'] = array('exp',"IN ($list)");
        $map['hot'] = 0;
        $map['is_delete']= 0;
        $datas = D('question')->where($map)->limit(5)->select();
        //查询页面话题(那些标签)
        $row = D('user_topic')->where(array('user_id'=>$_SESSION['home']['id']))->select();

        $jk = [];
        foreach ($row as $val) {
            $jk[] = $val['topic_id'];
        }
        $run = implode(',',$jk);

        if ($run == null) {
            $run = '1,2,3,4,5';
        }

        $btm['id'] = array('exp',"IN($run)");
        // var_dump($btm);
        $dds = D('topics')->where($btm)->select();
         // echo $dds = D('topics')->field('name')->where($btm)->select(false);
        // var_dump($datas);
         // var_dump($dds);
        $this->assign('row',$dds);
        $this->assign('list',$datas);
        $this->assign('title','回答问题');
        $this->display('Question/index');

    }

    //点击全部问题,显示出来
    public function quanbu(){
        // $list = M('question')->where(array('hot'=>0,'is_delete'=>0))->select();
        $list = M('question')->where(array('display'=>1))->order('datetime DESC')->limit(0,20)->select();
        $this->ajaxReturn($list);
    }
    //点击热门问题 显示出来
    public function remen(){
        // $list = M('question')->where(array('hot'=>1,'is_delete'=>0))->select();
        $list = M('question')->where(array('display'=>1))->order('answer_num DESC')->limit(0,20)->select();
        $this->ajaxReturn($list);
    }
    
    //点击×消失那条问题
    public function xiaoshi(){
        $list = M('question');
        $list->is_delete = '1';
        $list->where(array('id'=>$_GET['id']))->save();

    }
    //全站热门那块
    public function hot(){
        // $question = D('question')->where(array('hot'=>1))->select();
        $question = M('question')->where(array('display'=>1))->order('answer_num DESC')->limit(0,20)->select();
        $this->assign('title','回答问题');
        $this->assign('list',$question);
        $this->display('Question/hot');
    }

    public function invited(){
      $this->assign('title','回答问题');
        $this->display('Question/invited');
    }


    public function following(){
        $list = M('user_question')->where(array('user_id'=>$_SESSION['home']['id']))->select();
        // var_dump($list);
        $row = [];

        foreach ($list as $value) {
           $row[]=$value['question_id'];
        }
        // var_dump($row);
        $rows = implode(',',$row);
        // var_dump($rows);
        if(empty($rows)){
            $this->error('没有关注...');
            exit;
        }

        $map['id'] = array('exp',"IN ($rows)");

        // var_dump($map);
        $rowss = M('question')->where($map)->select();
        // echo D('question')->getLastSql();
        // var_dump($rowss);

        $this->assign('rowss',$rowss);
        $this->assign('title','我关注的问题-知乎');
        $this->display('Question/following');
    }

    public function quguan(){

        $map['user_id'] = $_SESSION['home']['id'];
        $map['question_id'] = $_GET['id'];
        // var_dump($map);
        $list = M('user_question')->where($map)->delete();

    }

    public function  guan(){
        $data=array();
        $data['user_id'] = $_SESSION['home']['id'];
        $data['question_id'] = $_GET['id'];
        // echo M('user_question')->where($data)->select(false);
        if(M('user_question')->where($data)->select() == null) {
            $data['datetime'] = date('Y-m-d H:i:s');
            $list = M('user_question')->add($data);
        }
    }





    // 单个问题详情页面
    public function detail()
    {
        // 获取 用户传递的 问题 数据
        if (empty($_GET['qid']) || intval($_GET['qid'])<=0) {
            $this->error('错误的问题id...请联系管理员!');
            exit();
        }
        $qid = intval($_GET['qid']);
        // V($qid);
        $q = M('question');

        //查询问题的关注人数,写入数据库
        $uq = M('user_question');
        $attend_num = $uq->where(array('question_id'=>$qid))->count('user_id');
        // 如果查不到, 则为0
        if (empty($attend_num)){
            $attend_num = 0;
        }
        $data['attend_num'] = $attend_num;
        $q->where('id=' . $qid)->save($data);

        // 查询问题基础信息
        $qinfo = $q->field('id, name, content, topic_id, topic_name, answer_num')->where('id='.$qid)->select();
        // var_dump($qinfo);

        // 清空data 便于查询数据接收
        $data = null;

        // 先判断有无答案 id 输入, 有则查询单独的答案
        if (!empty($_GET['aid']) && intval($_GET['aid'])>0) {
            $aid = intval($_GET['aid']);

            $where = "a.id='{$aid}' AND u.id=a.user_id AND a.display=1" ;
            $data = $q->field('a.id aid, a.filename afile, a.user_id auid, a.image aimg, a.discuss_number adum, a.zan azan, a.datetime atime, u.name uname, u.image uimg')->table('zhihu_answer a, zhihu_user u')->where($where)->select();
            // var_dump($data);
            if (!empty($data[0])) {
                // 拼接回答文件路径
                $path = './Data/'.substr($data[0]['afile'],0,4).'/'.substr($data[0]['afile'], 4,2).'/'.substr($data[0]['afile'],6,2).'/'.$data[0]['afile'];
                // echo $path;
                $data[0]['acontent'] = html_entity_decode(file_get_contents($path));
                $data[0]['acontent'] = preg_replace('/width=".*?"/', 'width="100%"', $data[0]['acontent']);
                $data[0]['acontent'] = str_replace('alt=""', 'width="100%"', $data[0]['acontent']);

            }

        } else {

            // 获取用户分页数据
            if (empty($_GET['p']) || intval($_GET['p'])<=0) {
                $p = 1;
            } else {
                $p = intval($_GET['p']);
            }

            $where = "a.question_id='{$qid}' AND u.id=a.user_id AND a.display=1" ;
            $data = $q->field('a.id aid, a.filename afile, a.user_id auid, a.image aimg, a.discuss_number adnum, a.zan azan, a.datetime atime, u.name uname, u.image uimg')->table('zhihu_answer a, zhihu_user u')->where($where)->order('a.datetime DESC')->page($p, 10)->select();
            // 获取 对应答案的内容
            $path = ''; //占位
            for ($i=0; $i <= count($data); $i++) { 
                if (!empty($data[$i])) {
                    // 拼接回答文件路径
                    $path = './Data/'.substr($data[$i]['afile'],0,4).'/'.substr($data[$i]['afile'], 4,2).'/'.substr($data[$i]['afile'],6,2).'/'.$data[$i]['afile'];
                    // echo $path;
                    $data[$i]['acontent'] = html_entity_decode(file_get_contents($path));
                    // $data[$i]['acontent'] =  preg_replace('/\.\.\/\.\.\/\.\.\//', '../../../../../', $data[$i]['acontent']);
                    // $data[$i]['acontent'] = strip_tags(html_entity_decode(file_get_contents($path, null, null , 0, 500))).'...';
                    $data[$i]['acontent'] = preg_replace('/width=".*?"/', 'width="100%"', $data[$i]['acontent']);
                    $data[$i]['acontent'] = str_replace('alt=""', 'width="100%"', $data[$i]['acontent']);
                }
            }
            // var_dump($data);
        } //结束else判断区间

        $die = M('question')->where(array('topic_id'=>$qinfo[0]['topic_id']))->order('id desc')->limit(3)->select();
        // var_dump($die);
         $abc = M('store')->where(array('user_id'=>$_SESSION['home']['id']))->select();
        // var_dump($abc);
        $this->assign('abc',$abc);
        $this->assign('die',$die);
        $this->assign('p', $p+1);
        $this->assign('qinfo', $qinfo);
        $this->assign('data', $data);
        $this->display('Question/detail');
    }



    public function jiashou(){
        // var_dump($_GET);
        $qun['user_id'] = $_SESSION['home']['id'];
        $qun['answer_id'] = $_GET['aid'];
        $qun['store_id']=$_GET['sid'];
       $wen = M('collect')->add($qun);
    }

}
